﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Fluid.Controls;
using System.Drawing;
using TouchRC.ListBoxes;
using TouchRC.Classes;
using TouchRC.Logic;
using TouchRC.BarManagers;
using TouchRC.Templates;
using TouchRC.MetadataCaching;
using System.IO;

namespace TouchRC
{
    public class ComputerSelectionPanel : FluidPanelBase
    {
        #region Private Members

        private static ComputerSelectionPanel instance;

        #region Fluid Controls

        private ComputerListBox m_ComputerListBox;

        #endregion

        #endregion

        public override void AddControls()
        {
            Controls.Add(m_ComputerListBox);
        }

        public override void SetControlPositions(ScreenOrientation screenOrientation)
        {
            int listboxWidth = ScreenWidth - (int)(ScreenWidth * 0.2);
            int margin = (ScreenWidth - listboxWidth) / 2;

            m_ComputerListBox.Bounds = new Rectangle(margin, BaseControlHeight, listboxWidth, ScreenHeight - (2 * BaseControlHeight));
        }

        public override void InitPanelControls()
        {
            m_ComputerListBox = new ComputerListBox();
        }

        public override void RefreshValues()
        {
            m_ComputerListBox.EditMode = false;
            m_ComputerListBox.UpdateData();
            Invalidate();
        }

        public override List<BarOption> GetBarOptions()
        {
            List<BarOption> options = new List<BarOption>();

            BarOption editComputers = new BarOption();
            editComputers.Image = ImageFactory.GetBitmap(ImageTypes.Cogs);
            editComputers.Click += new EventHandler<EventArgs>(editComputers_Click);

            BarOption addOption = new BarOption();
            addOption.Image = ImageFactory.GetBitmap(ImageTypes.Plus);
            addOption.Click += new EventHandler<EventArgs>(m_AddButton_Click);

            options.Add(editComputers);
            options.Add(addOption);

            return options;
        }

        public override void Back()
        {
        }

        void editComputers_Click(object sender, EventArgs e)
        {
            HideOptionsBar();

            MessageDialog.Show("Not Implemented yet.", "OK", null);
        }

        protected override void Initialize()
        {
            base.Initialize();
        }
        protected override void InitControl()
        {
            HeaderText = "Choose Computer";

            EnableNowPlayingGesture = false;
            EnableRemoteControlGesture = false;

            base.InitControl();
            instance = this;
            this.Bounds = AvailableScreen;

            m_ComputerListBox.ItemClick += new EventHandler<ListBoxItemEventArgs>(m_ComputerListBox_ItemClicked);
            m_ComputerListBox.Anchor = AnchorAll;
            m_ComputerListBox.DataBound += new EventHandler<TemplateEventArgs>(m_ComputerListBox_DataBound);
            base.ShowMenuButton = true;
        }

        void m_AddButton_Click(object sender, EventArgs e)
        {
            HideOptionsBar();
            ComputerConfigPanel ccp = new ComputerConfigPanel(ComputerConfigState.New);
            ccp.ShowMaximized(ShowTransition.FromBottom);
        }

        public override void Focus()
        {
            m_ComputerListBox.Focus();
        }

        #region Public Accessors

        public static ComputerSelectionPanel Instance
        {
            get
            {
                if (instance == null) instance = new ComputerSelectionPanel();
                return instance;
            }
        }

        public List<ComputerListBoxItem> ComputerList
        {
            get
            {
                return m_ComputerListBox.Computers;
            }
        }

        #endregion

        void m_ComputerListBox_DataBound(object sender, TemplateEventArgs e)
        {
            throw new NotImplementedException();
        }

        void m_ComputerListBox_ItemClicked(object sender, ListBoxItemEventArgs e)
        {
            ComputerListBoxItem item = e.Item as ComputerListBoxItem;
            if (item != null)
            {
                MainMenuPanel panel = MainMenuPanel.GenerateMainMenuPanel(item);

                panel.RefreshValues();
                panel.ShowMaximized();
                panel.SelectFirstOption();
            }
            else
            {
                MessageDialog.Show("Could not establish connection", "OK", null);
            }
        }

        
    }
}
